<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/11/21
 * Time: 3:57 PM
 */

namespace Biz\module\Decoupling\order;

use Biz\module\Decoupling\AbstractMain;
use Doraemon\tools\Log as tLog;
use Biz\model\GoodsExtendInfo as mGoodsExtendInfo;
use Biz\model\trade\OrderOperateLog as mOrderOperateLog;
use Biz\service\Event;
use Doraemon\amqp\Queue as aQueue;
use Biz\pockets\constInterface\ActivityInfo as constActivityInfo;
use Biz\service\Activitys;
use Biz\model\trade\Order as mOrder;
use Biz\module\Classes\Cart as clsCart;


class CreateOrder extends AbstractMain
{
    const NAME = '创建订单成功';

    public function main($message, $data)
    {

        //解析 json 并赋值变量
        $appendData = [
            'eventId' => $data['header']['event_id'],
            'addTime' => $data['header']['addtime'],
            'uid' => $data['body']['uid'],
            'fromSource' => $data['body']['from_source'],
            'orderList' => $data['body']['order_list'],
            'goodsIdList' => [],
            'orderSkuList' => [],
            'skuIds' => [],
            'orderActivityInsert' => [],
        ];



        $orderNos = array_column($appendData['orderList'], 'order_no');
        $skuInfoList= array_column($appendData['orderList'], null, 'order_no');
        $orderList = mOrder::getOrderInfoList(['order_no' => $orderNos]);


        foreach ($orderList as $orderInfo) {
            foreach ($skuInfoList[$orderInfo['order_no']]['sku_list'] as $skuInfo) {
                //组装数据
                $appendData['skuIds'][] = $skuInfo['sku_id'];

                if (!empty($skuInfo['promo_type']) && !empty($skuInfo['promo_id'])) {
                    $appendData['orderActivityInsert'][] = [
                        'order_no' => $skuInfo['order_no'],
                        'activity_id' => $skuInfo['promo_id'],
                        'activity_type' => $skuInfo['promo_type'],
                        'sku_id' => $skuInfo['sku_id'],
                        'origin_price' => $skuInfo['sell_price'] * $skuInfo['buy_num'],
                        'purchase_price' => $skuInfo['buy_price'] * $skuInfo['buy_num']
                    ];
                }

            }

            if (!empty($orderInfo['full_shipping_plan_id'])) {
                $appendData['orderActivityInsert'][] = [
                    'order_no' => $orderInfo['order_no'],
                    'activity_id' => $orderInfo['full_shipping_plan_id'],
                    'activity_type' => constActivityInfo::ACTIVITY_TYPE_FULL_SHIPPING,
                    'sku_id' => 0,
                    'origin_price' => $orderInfo['shipping_fee'],
                    'purchase_price' => $orderInfo['shipping_amount']
                ];
            }
        }

        $appendData['orderList'] = $orderList;
        $appendData['skuInfoList'] =$skuInfoList;

        $this->appendData($appendData);

    }


    protected function _exec_order_operate_log()
    {


        $goodsIdList = [];

        foreach ($this->data['orderList'] as $orderInfo) {

            foreach ($this->data['skuInfoList'][$orderInfo['order_no']]['sku_list'] as $skuInfo) {
                //记录下单时商品的详情描述需要的变量
                $goodsIdList[$skuInfo['order_no']][] = $skuInfo['goods_id'];
            }

            //下单时记录商品的详情描述信息
            $operateData = [];
            if (!empty($goodsIdList[$orderInfo['order_no']])) {

                $goodsExtends = mGoodsExtendInfo::select(['goods_id', 'content'], ['goods_id' => $goodsIdList[$orderInfo['order_no']]]);

                $operateData['credentials'] = array_column($goodsExtends, null, 'goods_id');

            }


            //记录订单操作日志
            mOrderOperateLog::createLog($orderInfo['shop_id'], $orderInfo['order_no'], mOrderOperateLog::TRADE_TYPE_ORDER, [
                'operate_type' => mOrderOperateLog::OPERATE_TYPE_CREATE_ORDER,
                'operate_user_type' => mOrderOperateLog::OPERATE_USERTYPE_USER,
                'operate_uid' => $this->data['uid'],
            ], '', '提交订单', '', $operateData);


            $this->appendLog('记录订单操作日志', array_merge($this->data, $operateData));
        }

    }

    protected function _exec_event_stock_incr()
    {
        foreach ($this->data['orderList'] as $orderInfo) {

            foreach ($this->data['skuInfoList'][$orderInfo['order_no']]['sku_list'] as $skuInfo) {

                //记录库存扣减的事件
                Event::push(Event::ORDER_CREATE_STOCK_DECR_SUCCESS, $eventData = [
                    'uid' => $this->data['uid'],
                    'order_no' => $skuInfo['order_no'],
                    'shop_id' => $skuInfo['shop_id'],
                    'goods_id' => $skuInfo['goods_id'],
                    'sku_id' => $skuInfo['sku_id'],
                    'stock' => $skuInfo['buy_num'],
                    'status' => 1,
                    'uniq_idx' => ['order_no', 'event_tag','sku_id']
                ], $this->data['eventId']);

                $this->appendLog('发送库存扣减事件成功', array_merge($this->data, $eventData));

            }
        }
    }

    protected function _exec_event_integral_send()
    {
        foreach ($this->data['orderList'] as $orderInfo) {

            //积分 平台券 店铺券使用事件
            if ($orderInfo['integral'] > 0 && $orderInfo['integral_amount'] > 0) {
                Event::push(Event::ORDER_CREATE_USE_INTEGRAL_SUCCESS, $eventData = [
                    'uid' => $this->data['uid'],
                    'order_no' => $orderInfo['order_no'],
                    'integral' => $orderInfo['integral'],
                    'integral_amount' => $orderInfo['integral_amount'],
                    'status' => 1,
                    'uniq_idx' => ['order_no', 'event_tag']
                ], $this->data['eventId']);

                $this->appendLog('发送积分使用事件', array_merge($this->data, $eventData));

            }


        }

    }

    protected function _exec_event_coupon_send()
    {
        foreach ($this->data['orderList'] as $orderInfo) {


            if ($orderInfo['coupon_id'] > 0 && $orderInfo['coupon_amount'] > 0) {
                Event::push(Event::ORDER_CREATE_USE_COUPON_SUCCESS, $eventData = [
                    'uid' => $this->data['uid'],
                    'order_no' => $orderInfo['order_no'],
                    'coupon_vid' => $orderInfo['coupon_vid'],
                    'coupon_id' => $orderInfo['coupon_id'],
                    'coupon_amount' => $orderInfo['coupon_amount'],
                    'status' => 1,
                    'uniq_idx' => ['order_no', 'event_tag']
                ], $this->data['eventId']);

                $this->appendLog('发送平台券使用事件', array_merge($this->data, $eventData));

            }


        }
    }

    protected function _exec_event_s_coupon_send()
    {
        foreach ($this->data['orderList'] as $orderInfo) {

            if ($orderInfo['s_coupon_id'] > 0 && $orderInfo['s_coupon_amount'] > 0) {
                Event::push(Event::ORDER_CREATE_USE_S_COUPON_SUCCESS, $eventData = [
                    'uid' => $this->data['uid'],
                    'shop_id'=>$orderInfo['shop_id'],
                    'order_no' => $orderInfo['order_no'],
                    's_coupon_vid' => $orderInfo['s_coupon_vid'],
                    's_coupon_id' => $orderInfo['s_coupon_id'],
                    's_coupon_amount' => $orderInfo['s_coupon_amount'],
                    'status' => 1,
                    'uniq_idx' => ['order_no', 'event_tag']
                ], $this->data['eventId']);

                $this->appendLog('发送商家优惠券使用事件', array_merge($this->data, $eventData));

            }

        }
    }

    protected function _exec_delete_from_cart()
    {
        //删除购物车
        if ($this->data['fromSource'] == mOrder::SOURCE_CART) {
            (new clsCart())->delCart($this->data['uid'], $this->data['skuIds'], 0, 'afterCreateOrder');

            $this->appendLog('删除购物车', array_merge($this->data, ['sku_ids' => $this->data['skuIds']]));

        }
    }

    protected function _exec_activity_add_log()
    {
        //添加活动订单log
        if ($this->data['orderActivityInsert']) {
            Activitys::addLog($this->data['orderActivityInsert']);

            $this->appendLog('添加活动日志', array_merge($this->data, ['activity_log' => $this->data['orderActivityInsert']]));

        }
    }


}